<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.Hashtable"%>
<%@ page import="java.util.Enumeration"%>
<%@ page import="projects.milestone" %>
<%@ page import="projects.task" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="milestone" class="projects.milestone"></jsp:useBean>
<jsp:setProperty property="*" name="milestone"/>
<jsp:useBean id="task" class="projects.task"></jsp:useBean>
<jsp:setProperty property="*" name="task"/>
<html>
<%
if (request.getParameter("id") == null){
	response.sendRedirect("../General Info/dashboard.jsp");
}
else if (session.getAttribute("loggedIn") == null) {
	response.sendRedirect("../User/index.jsp");
}
else{
	milestone mil = new milestone();
	mil.setMilestoneId(Integer.parseInt(request.getParameter("id")));
	ArrayList<String> info = new ArrayList<String>(); 
	info = mil.queryMilestoneInfo();
	task tsk = new task();
	tsk.setMilestoneId(Integer.parseInt(request.getParameter("id")));
	Hashtable<Integer, ArrayList<String>> inProgressTasks = tsk.queryTasksByMilestoneId();
	Integer projectId = mil.queryProjectId();
%>
<%ArrayList<Integer> neww = new ArrayList<Integer>();
ArrayList<Integer> assigned = new ArrayList<Integer>();
ArrayList<Integer> resolved = new ArrayList<Integer>();
ArrayList<Integer> closed = new ArrayList<Integer>();
ArrayList<Integer> reopened = new ArrayList<Integer>();

Enumeration<Integer> taskKeysEnumb = inProgressTasks.keys();
ArrayList<Integer> taskKeysb = new ArrayList<Integer>();
while(taskKeysEnumb.hasMoreElements()){
	taskKeysb.add(taskKeysEnumb.nextElement());
}
for(Integer key : taskKeysb) {
	
	ArrayList<String> inProgressTsk = new ArrayList<String>();
	inProgressTsk = inProgressTasks.get(key);
	if(Integer.parseInt(inProgressTsk.get(3).toString()) == 4){
		closed.add(key);
	}
	else if(Integer.parseInt(inProgressTsk.get(3).toString()) == 1){
		neww.add(key);
	}
	else if(Integer.parseInt(inProgressTsk.get(3).toString()) == 2){
		assigned.add(key);
	}
	else if(Integer.parseInt(inProgressTsk.get(3).toString()) == 3){
		resolved.add(key);
	}
	else if(Integer.parseInt(inProgressTsk.get(3).toString()) == 5){
		reopened.add(key);
	}
	
}
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="../General Info/css.css" />
<title>Work Package</title>
<script src="../General Info/amcharts.js" type="text/javascript"></script>
<script src="../General Info/raphael.js" type="text/javascript"></script>        
	    
        <script type="text/javascript">
		var neww = <%out.println(neww.size()); %>;
		var assigned = <%out.println(assigned.size()); %>
		var resolved = <%out.println(resolved.size()); %>
		var closed = <%out.println(closed.size()); %>
		var reopened = <%out.println(reopened.size()); %>
		var chart;

		var chartData = [{country:"New", visits:neww, color:"#FF0F00"},
				{country:"Assigned",visits:assigned,color:"#FF6600"},
				{country:"Resolved",visits:resolved,color:"#9CBF1B"},
				{country:"Closed",visits:closed,color:"#FCD202"},
				{country:"Reopened",visits:reopened,color:"#F8FF01"}];


        window.onload = function() {
			chart = new AmCharts.AmSerialChart();
			chart.dataProvider = chartData;
			chart.categoryField = "country";
			chart.marginTop = 25;
			chart.marginBottom = 80;
			chart.marginLeft = 20;
			chart.marginRight = -10;
			chart.startDuration = 1;

			var graph = new AmCharts.AmGraph();
			graph.valueField = "visits";
			graph.colorField = "color";
			graph.balloonText="[[category]]: [[value]]";
			graph.type = "column";
			graph.lineAlpha = 0;
			graph.fillAlphas = 0.8;
			chart.addGraph(graph);

			var catAxis = chart.categoryAxis;
			catAxis.gridPosition = "start";
			catAxis.autoGridCount = true;

			chart.write("chartdiv");
		}
	    </script>
</head>



<body>
<jsp:include page="../General Info/header.jsp"/>

<%/* <!-- -----------------------------------------------> Submeniu <-------------------------------------------------- --> */%>
	<div class="submenu">
		<ul class="submenuBar">
			<%
			String taskLink = "../Tasks/createNewTask.jsp?id="+ mil.getMilestoneId();
			String projectLink = "../Projects/projectDashboard.jsp?id="+ projectId;
			String milestoneLink = "../Milestones/milestone.jsp?id="+ mil.getMilestoneId();
			%>
			<li><a href=<% out.println(projectLink); %> class="submenuButton">Project</a></li>
			<li><a href=<% out.println(milestoneLink); %> class="submenuButton">Work Packages</a></li>
			<li><a href=<% out.println(taskLink); %> class="submenuButton">Add Task</a></li>
		</ul>
	</div>
	
<% /* ------------------------------------------------------------------------------------------------------------------------------- */ %>
	<div class="body">
	<div class="up">
	<div class = "projects">
		<div class = "projectsHeader">Work Package Information</div>
		<div id="chartdiv" style="width:250px; height:300px; float: left;"></div>
		<table style="float: right">
					<tr>
						<td class="labelName">Milestone Name:</td>
						<td class="labelValue"><% out.println(info.get(1)); %></td>
					</tr>
					<tr>
						<td class="labelName">Task Description:</td>
						<td class="labelValue"><%out.println(info.get(2)); %></td>
					</tr>
					<tr>
						<td class="labelName">Start date:</td>
						<td class="labelValue"><%out.println(info.get(7)); %></td>
					</tr>
					<tr>
						<td class="labelName">End date:</td>
						<td class="labelValue"><%out.println(info.get(8)); %></td>
					</tr>
					<tr>
						<td class="labelName">Milestone effort:</td>
						<td class="labelValue"><%out.println(info.get(3)); %></td>
					</tr>
					<tr>
						<td class="labelName">Milestone State:</td>
						<td class="labelValue"><%out.println(info.get(4)); %></td>
					</tr>
					<tr>
						<td class="labelName">Milestone State Reason:</td>
						<td class="labelValue"><%out.println(info.get(5)); %></td>
					</tr>
					<tr>
						<td class="labelName">Milestone estimated cost:</td>
						<td class="labelValue"><%out.println(info.get(6)); %></td>
					</tr>
				</table>
				<%
			if(session.getAttribute("postback")=="true")
			{
				if (session.getAttribute("createMilestone")=="true") { %>
					<div class="successfulChange" id="successfulChange">New milestone added successfully!!</div>
					<%session.setAttribute("createMilestone", null);
					session.setAttribute("postback", null);
				}
				else if(session.getAttribute("createMilestone")=="false") {
					%><div class="notsuccessfulChange" id="notsuccessfulChange">Could not add milestone!!!</div>
					<%session.setAttribute("createMilestone", null);
					session.setAttribute("postback", null);
				}
			}
			else{
				%><div class="noChange" id="noChange"></div>	
			<%}%>
	</div>
	
	<div class = "activityb">
			<form name="changeStateForm" onsubmit="" method="GET" action="../Milestones/changeMilestoneState.jsp">
					<% session.setAttribute("milestoneId", mil.getMilestoneId()); %>
					<table>
						<tr>
							<td>Change Milestone State: </td>
							<td><select name="milestoneStateId" id="milestoneStateId">
							<option value="1">GRAY</option>
							<option value="2">RED</option>
							<option value="3">GREEN</option>
							<option value="4">YELLOW</option>
							</select></td>
							</tr>
						<tr>
							<td>Reason: </td>	
							<td>
							<textarea rows="5" cols="40" id="milestoneStateReason" name="milestoneStateReason"></textarea>
							</td>
						</tr>
						<tr>
							<td><input type="submit" class="inputButton" value="Change" /></td>
						</tr>
					</table>
					<% 
					if(session.getAttribute("postback")=="true")
					{
						if (session.getAttribute("changeMilestoneState")=="true") { %>
							<div class="successfulChange" id="successfulChange">Milestone State changed successfully!!</div>
							<%session.setAttribute("changeMilestoneState", null);
							session.setAttribute("postback", null);
						}
						else if(session.getAttribute("changeMilestoneState")=="false") {
							%><div class="notsuccessfulChange" id="notsuccessfulChange">Could not change status and resolution!!!</div>
							<%session.setAttribute("changeMilestoneState", null);
							session.setAttribute("postback", null);
						}
					}
					else{
						%><div class="noChange" id="noChange"></div>	
					<%}%>
				</form>
	</div>
	</div>
	
	<div class="down">
		<div class = "upcomingActivities">
			<div class = "projectsHeader">Completed Tasks</div>
			<table style="float: right">
			<tr>
				<td class="projectTableHeader"></td>
				<td class="projectTableHeader">Task Number</td>
				<td class="projectTableHeader">Task Name</td>
				<td class="projectTableHeader">Days till deadline:</td>
				<td class="projectTableHeader">Status</td>
			</tr>
				<%
		Enumeration<Integer> taskKeysEnum = inProgressTasks.keys();
		ArrayList<Integer> taskKeys = new ArrayList<Integer>();
		while(taskKeysEnum.hasMoreElements()){
			taskKeys.add(taskKeysEnum.nextElement());
		}
		int color = 0;
		for(Integer key : taskKeys) {
			
			ArrayList<String> inProgressTsk = new ArrayList<String>();
			inProgressTsk = inProgressTasks.get(key);
			String taskPageLink = "../Tasks/task.jsp?id=" + key;   /* link + taskId */ 
				if( Integer.parseInt(inProgressTsk.get(3).toString()) == 4){	
					color = color + 1;
					if(color % 2 ==0){
						%>
						<tr class="even">
				<td class="cell"></td>
				<td class="cell"><a href="<% out.println(taskPageLink); %>"> <% out.println(key); %></a></td>   
				<td class="cell"><% out.println(inProgressTsk.get(0)); %></td>
				<td class="cell"><% out.println(inProgressTsk.get(2)); %></td>
				<td class="cell"><% out.println(inProgressTsk.get(3)); %></td>
			</tr>	
					<% }
					else{
						%>
						<tr class="odd">
				<td class="cell"></td>
				<td class="cell"><a href="<% out.println(taskPageLink); %>"> <% out.println(key); %></a></td>   
				<td class="cell"><% out.println(inProgressTsk.get(0)); %></td>
				<td class="cell"><% out.println(inProgressTsk.get(2)); %></td>
				<td class="cell"><% out.println(inProgressTsk.get(3)); %></td>
			</tr>	
					<%}}}
		%>
		</table>
		</div>
		
		<div class="administrationb">
			<div class = "projectsHeaderb">Tasks in Progress</div>
			
			<table style="float: right; margin-top: -11px;">
			<tr>
				<td class="projectTableHeader"></td>
				<td class="projectTableHeader">Task Number</td>
				<td class="projectTableHeader">Task Name</td>
				<td class="projectTableHeader">Days till deadline:</td>
				<td class="projectTableHeader">Status</td>
			</tr>
				<%
			while(taskKeysEnum.hasMoreElements()){
			taskKeys.add(taskKeysEnum.nextElement());
		}
				int colorB = 0;
		for(Integer key : taskKeys) {
			ArrayList<String> inProgressTsk = new ArrayList<String>();
			inProgressTsk = inProgressTasks.get(key);
			String taskPageLink = "../Tasks/task.jsp?id=" + key;   /* link + taskId */ 
				if( Integer.parseInt(inProgressTsk.get(3).toString()) != 4){	 		
					colorB = colorB + 1;
					if(colorB % 2 == 0){
						%>
						<tr class="even">
				<td class="cell"></td>
				<td class="cell"><a href="<% out.println(taskPageLink); %>"> <% out.println(key); %></a></td>   
				<td class="cell"><% out.println(inProgressTsk.get(0)); %></td>
				<td class="cell"><% out.println(inProgressTsk.get(2)); %></td>
				<td class="cell"><% out.println(inProgressTsk.get(3)); %></td>
			</tr>	
					<% }
					else{
						%>
						<tr class="odd">
				<td class="cell"></td>
				<td class="cell"><a href="<% out.println(taskPageLink); %>"> <% out.println(key); %></a></td>   
				<td class="cell"><% out.println(inProgressTsk.get(0)); %></td>
				<td class="cell"><% out.println(inProgressTsk.get(2)); %></td>
				<td class="cell"><% out.println(inProgressTsk.get(3)); %></td>
			</tr>	
					<%}}}
		%>			
			</table>
		</div>
	</div>
	<div class="push"></div>
</div>
<jsp:include page="../General Info/footer.jsp"/>
</body>
<%} %>
</html>